package com.chat.api.web.restctrl;


import com.chat.api.utils.ResultUtil;
import com.chat.api.utils.util.DateUtil;
import com.chat.api.utils.util.ParamUtil;
import com.chat.api.web.service.ApprovalService;
import com.chat.api.web.vo.ApprovalEntity;
import com.chat.api.web.vo.MessageEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.Date;

@Controller
@RequestMapping("/approval")
public class ApprovalCtrl {

    @Autowired
    private ApprovalService approvalService;

    @RequestMapping(value = "deploy",produces = "application/json",method = RequestMethod.GET)
    @ApiOperation("部署审批流程")
    public void deploy(HttpServletRequest request) {
        String flowName = "面试审批流程";
        Boolean result = approvalService.deployProcess(flowName);
    }

    @RequestMapping(value = "startApproval",produces = "application/json",method = RequestMethod.POST)
    @ApiOperation("启动审批流程")
    public MessageEntity startApproval(HttpServletRequest request,
                                       @RequestParam("name") String name,
                                       @RequestParam("iphone") String iphone,
                                       @RequestParam("email") String email,
                                       @RequestParam("job") String job,
                                       @RequestParam("score") Integer score,
                                       @RequestParam("startTime") String startTimeS,
                                       @RequestParam("endTime") String endTimeS) {
        ApprovalEntity approvalEntity = new ApprovalEntity();
        Date startTime = new Date();
        Date endTime = new Date();
        if (startTimeS!=null){
            startTime = DateUtil.strToDate(startTimeS, 6);
        }
        if (endTimeS!=null){
            endTime = DateUtil.strToDate(endTimeS, 6);
        }
        approvalEntity.setName(name);
        approvalEntity.setIphone(iphone);
        approvalEntity.setEmail(email);
        approvalEntity.setJob(job);
        approvalEntity.setScore(score);
        approvalEntity.setStartTime(startTime);
        approvalEntity.setEndTime(endTime);
        approvalService.startApproval(approvalEntity,request);
        return ResultUtil.success(request);
    }

    @RequestMapping(value = "findFlowImage",produces = "application/json",method = RequestMethod.POST)
    @ApiOperation("查看审批流程图")
    public MessageEntity findFlowImage(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam(value = "id") String id) {
        Long idL = ParamUtil.getLongParameter(id, -1L);
        return approvalService.getApprovalRate(idL,response);
    }

    @RequestMapping(value = "findFlowDetail",produces = "application/json",method = RequestMethod.POST)
    @ApiOperation("查看审批流程节点详情")
    public String findFlowDetail(HttpServletRequest request, ModelMap mm,
                                 @RequestParam("id") String ids) {
        Long id = ParamUtil.getLongParameter(request, "ids", -1);
        return approvalService.findFlowDetail(id,mm);
    }

    @RequestMapping(value = "approval",produces = "application/json",method = RequestMethod.POST)
    @ApiOperation("流程节点审批")
    public MessageEntity approval(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam("name") String name,
                                  @RequestParam("iphone") String iphone,
                                  @RequestParam("email") String email,
                                  @RequestParam("job") String job,
                                  @RequestParam("result") String result,
                                  @RequestParam("score") Integer score,
                                  @RequestParam("startTime") String startTimeS,
                                  @RequestParam("endTime") String endTimeS) {
        ApprovalEntity approvalEntity = new ApprovalEntity();
        Date startTime = new Date();
        Date endTime = new Date();
        if (startTimeS!=null){
            startTime = DateUtil.strToDate(startTimeS, 6);
        }
        if (endTimeS!=null){
            endTime = DateUtil.strToDate(endTimeS, 6);
        }
        approvalEntity.setName(name);
        approvalEntity.setIphone(iphone);
        approvalEntity.setEmail(email);
        approvalEntity.setJob(job);
        approvalEntity.setResult(Integer.parseInt(result));
        approvalEntity.setScore(score);
        approvalEntity.setStartTime(startTime);
        approvalEntity.setEndTime(endTime);
        return approvalService.approval(request,approvalEntity);
    }

    @RequestMapping(value = "findWithDown",produces = "application/json",method = RequestMethod.GET)
    @ApiOperation("获取待办流程列表")
    public String findWithDown(HttpServletRequest request, ModelMap mm) {
        return approvalService.findWithDown(request,mm);
    }

    @RequestMapping(value = "findFlowHistory",produces = "application/json",method = RequestMethod.POST)
    @ApiOperation("查看审批历史")
    public MessageEntity findFlowHistory(HttpServletRequest request,
                                         @RequestParam(value = "id") String id) {
        return approvalService.findFlowHistory(id);
    }

}
